[XPU] get_infer_param use inplace copy, remove block_tables abundant d2h copy#7431
[XPU] get_infer_param use inplace copy, remove block_tables abundant d2h copy#7431RuohengMa wants to merge 25 commits intoPaddlePaddle:developfrom
Conversation
|
Thanks for your contribution! |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #7431 +/- ##
==========================================
Coverage ? 74.15%
==========================================
Files ? 398
Lines ? 55011
Branches ? 8616
==========================================
Hits ? 40796
Misses ? 11481
Partials ? 2734
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
8c8f7ee to
7ff2c11
Compare
6d21f41 to
1dceb07
Compare
1dceb07 to
7d3c075
Compare
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 AI Code Review |
2026-04-21 10:07:18
📋 Review 摘要
PR 概述:将 XPU get_infer_param 算子改为 inplace copy 模式,并将 lod_to_slot_mapping 中的 slot_mapping 计算从 CPU 移至设备端执行,消除 block_tables 的冗余 D2H 拷贝。
变更范围:custom_ops/xpu_ops/(算子实现 + 测试)、fastdeploy/model_executor/(前处理 + forward_meta)
影响面 Tag:[XPU] [OP]
📝 PR 规范检查
PR 描述过于简略,Motivation 和 Modifications 部分仅重复了标题内容,未详细描述优化方法和影响面。
描述建议(可直接复制):
## Motivation
在 XPU 推理场景下,`get_infer_param` 每次调用都会重新分配输出 tensor 并对 block_tables 做 D2H 拷贝以计算 slot_mapping,影响推理性能。
## Modifications
1. 将 `get_infer_param` 算子由「分配新 tensor 作为输出」改为「接受预分配 tensor 进行 inplace 写入」,支持 cudagraph 模式下的 tensor 复用。
2. 将 `lod_to_slot_mapping` 中的 slot_mapping 计算逻辑从 CPU 迁移到 XPU 设备端,使用 `index_select` + `broadcast_mul` + `broadcast_add` 替代原有的 D2H + CPU 计算 + H2D 路径。
3. 更新 Python 调用侧(`xpu_pre_and_post_process.py`)和 `XPUForwardMeta`,增加 `init_inplace_tensor` 方法管理预分配 tensor 生命周期。
4. 更新所有相关测试以适配新接口。
问题
| 级别 | 文件 | 概述 |
|---|---|---|
| ❓ 疑问 | get_infer_param.cc:401 |
GetInferParamInferShape 函数签名与 PD_BUILD_OP 声明不匹配 |
详情:PD_BUILD_OP 现在声明了 26 个 Inputs 和 2 个 Attrs(block_size、num_speculative_tokens),但 GetInferParamInferShape 函数仅接收 4 个 shape 参数 + 1 个 int 参数(命名为 num_speculative_tokens)。如果该函数被框架通过 PD_INFER_SHAPE 宏调用(如静态图/编译模式),存在两个风险:
- 参数数量不匹配可能导致未定义行为;
- attrs 按序传递时,该 int 参数实际接收的是
block_size的值而非num_speculative_tokens,导致slot_mapping_dec的推断 shape 错误。
如果当前仅通过 pybind 调用(不走 PD_BUILD_OP 派发),可忽略此问题,但建议添加注释说明。
总体评价
优化思路清晰,通过 inplace tensor + 设备端计算有效减少了 D2H 拷贝和内存分配开销。测试文件同步更新,utils.py 抽取了公共初始化逻辑,代码可维护性良好。建议补充 PR 描述并确认 GetInferParamInferShape 在静态图模式下的兼容性。
Motivation
get_infer_param use inplace copy, remove block_tables abundant d2h copy
Modifications
get_infer_param use inplace copy, remove block_tables abundant d2h copy
Usage or Command
None
Accuracy Tests
None
Checklist
[FDConfig],[APIServer],[Engine],[Scheduler],[PD Disaggregation],[Executor],[Graph Optimization],[Speculative Decoding],[RL],[Models],[Quantization],[Loader],[OP],[KVCache],[DataProcessor],[BugFix],[Docs],[CI],[Optimization],[Feature],[Benchmark],[Others],[XPU],[HPU],[GCU],[DCU],[Iluvatar],[Metax]]pre-commitbefore commit.releasebranch, make sure the PR has been submitted to thedevelopbranch, then cherry-pick it to thereleasebranch with the[Cherry-Pick]PR tag.